<?php  if ( ! defined('ROOT_PATH')) exit('非法访问！！！');

class router
{
	public $aConfiger = '';
	public $aRouter = '';
	private $sSpace = '';
	private $sAction = '';
	private $sSuffix = '';
	private $sCharset = '';
	private $sScriptSuffix = '';
	function __construct()
	{
		$this->aConfiger = &get_config('configer');
		$this->aRouter = &get_config('router');
	}
	
	function execute()
	{
		$this->sSpace = get_or_post($this->aConfiger['url_space']);
		$this->sAction = get_or_post($this->aConfiger['url_action']);
		$this->sSuffix = get_or_post($this->aConfiger['url_suffix']);
		if ($this->sSpace == '' && $this->sAction == '' && $this->sSuffix == '') { #space、action、suffix都没有，则可能是命令行执行的
			if (isset($GLOBALS['argv'])) {
				$aArgv = $GLOBALS['argv'];
				$this->sSpace = isset($aArgv[$this->aConfiger['argv_space']]) && $aArgv[$this->aConfiger['argv_space']] != $this->aRouter['argv_space_defaultmark'] ? $aArgv[$this->aConfiger['argv_space']] : $this->aRouter['default_space'];
				$this->sAction = isset($aArgv[$this->aConfiger['argv_action']]) && $aArgv[$this->aConfiger['argv_action']] != $this->aRouter['argv_action_defaultmark'] ? $aArgv[$this->aConfiger['argv_action']] : $this->aRouter['default_action'];
				$this->sSuffix = isset($aArgv[$this->aConfiger['argv_suffix']]) && $aArgv[$this->aConfiger['argv_suffix']] != $this->aRouter['argv_suffix_defaultmark'] ? $aArgv[$this->aConfiger['argv_suffix']] : $this->aRouter['default_suffix'];
			}
		} else{
			$this->sSpace = $this->sSpace=='' ? $this->aRouter['default_space'] : $this->sSpace;
			$this->sAction = $this->sAction=='' ? $this->aRouter['default_action'] : $this->sAction;
			$this->sSuffix = $this->sSuffix=='' ? $this->aRouter['default_suffix'] : $this->sSuffix;
		}
		$this->sScriptSuffix = '_'.$this->sSuffix;
		if ($this->sSpace == '') {
			show_error('要执行的脚本所在的空间必需有');
		}
		$this->select_action();
	}
	
	function select_action()
	{
		if ($this->sAction == '') { #没有action，则执行space目录下的所有后缀为$this->sScriptSuffix脚本
			if ($this->sSpace) {
				$this->sSpace = str_replace(PATH_CUT,'/',$this->sSpace).'/';
			}
			$sTmpDir = APP_PATH.$this->sSpace;
//			echo $sTmpDir;
			if (is_dir($sTmpDir)) {
				$aTmpFile = my_scandir($sTmpDir);
				if (!empty($aTmpFile)) {
					foreach ($aTmpFile as $v) {
						//得到文件名
						$sFileName = substr(basename($v),0,(strlen(basename($v))-strlen(SCRIPT_TYPE)-1));
						if (preg_match('/(.*?)'.$this->sScriptSuffix.'$/i',$sFileName,$aTmp2)) {
							$this->call_execute_class($v,$aTmp2[1]);
						}
					}
				} else {
					show_error('脚本空间没有可执行的脚本！');
				}
			} else {
				show_error('没您指定的脚本空间！');
			}
		} else if ($this->sAction != '') { #action有，则执行space下的action脚本
			if ($this->sSpace) {
				$this->sSpace = str_replace(PATH_CUT,'/',$this->sSpace).'/';
			}
			$sTmpDir = APP_PATH.$this->sSpace;
			$sTmpScript = $sTmpDir.$this->sAction.$this->sScriptSuffix.EXT;
			if (!file_exists($sTmpScript)) {
				show_error('没您指定的脚本！');
			}
			$this->call_execute_class($sTmpScript,$this->sAction);
		}
	}
	
	function call_execute_class($sPath,$sClassName)
	{
		require_once($sPath);
		$oTmp = new $sClassName();
		$oTmp->execute();
		$oTmp->__destruct();
	}
	
	function get_action()
	{
		return $this->sAction;
	}
	function get_space()
	{
		return $this->sSpace;
	}
}